#include "dpLibrary\dpWindows.h"
#include "dpLibrary\graphics.h"
#include "dpLibrary\dpShared.h"

#include "winlib.h"

#include "AFObjectFactory.h"
#include "AFGraphics.h" 


/*booleans for isColliding*/
  int isCollidingFromLeft = 0;
  int isCollidingFromRight = 0;
  int isCollidingFromTop = 0;
  int isCollidingFromBot = 0;

// Make this function work with any two sprites
//void isSpriteAbove(Sprite* character, Sprite* spriteList)
//{
//  // Iterates through the linked list to find
//  // an object with the same x-values as the player.
//  Sprite* currentSprite = spriteList;
//  Sprite* messageSprite = malloc(sizeof(Sprite));
//
//  messageSprite->color = 0xFF0000;
//  messageSprite->height = 20;
//  messageSprite->width = 20;
//  messageSprite->screen.X = 200;
//  messageSprite->screen.Y = 200;
//
//  while (currentSprite)
//  {
//    if ((currentSprite->world.X) < character->world.X && 
//      character->world.X < (currentSprite->world.X + currentSprite->width))
//    {     
//      DrawSprite(messageSprite);     
//    }
//
//    currentSprite = currentSprite->next;
//  } 
//}

//void MovementControl(Sprite *controlledSprite, Sprite *spriteList, float velocity, float acceleration)
//{
//  int time = TimeGetTime();
//  int timePressed;
//  controlledSprite->isControlled = 1; /* isControlled determines which sprite the camera should follow */
//	
//  if(keyboard.wKey)
//    {
//
//      MoveCamera(spriteListHead, 0, -2);
//    }
//    if(keyboard.aKey)
//    {
//      MoveCamera(spriteListHead, -2, 0);
//    }
//    if(keyboard.sKey)
//    {
//      MoveCamera(spriteListHead, 0, 2);
//    }
//    if(keyboard.dKey)
//    {
//      MoveCamera(spriteListHead, 2, 0);
//    }
//	switch (keyPress)
//	{
//		case 'W':
//          timePressed = TimeGetTime() - time;
//			velocity = velocity + acceleration * timePressed; 
//			controlledSprite->position.X = controlledSprite->position.Y + velocity * timePressed;
//          time = TimeGetTime();
//			break;
//		case 'S':
//			velocity = velocity - acceleration * time 
//			controlledSprite->position.X = controlledSprite->position.Y + velocity * time;
//			break;
//		case 'A':
//			velocity = velocity - acceleration * time 
//			controlledSprite->position.X = controlledSprite->position.X + velocity * time;
//			break;
//		case 'D':
//			velocity = velocity + acceleration * time 
//			controlledSprite->position.X = controlledSprite->position.X + velocity * time;
//			break;
//		default:
//			MoveCamera(spriteListHead, 0, 0);
//			break;
//	}
//	controlledSprite->isControlled = 0;
//}


/* returns 1 if controlledSprite detects collision */
int CollisionDetection(Sprite *controlledSprite, Sprite *spriteList)
{
  /* Square-Square Collision */
  /* Coordinates of Left, Right, Top, Bottom borders of the two squares */
  int currentL;
  int currentR;
  int currentT;
  int currentB;
  int controlledL;
  int controlledR;
  int controlledT;
  int controlledB;
  int collidingOffset = 4;

  Sprite *currentSprite = spriteList;

  while (currentSprite)
  {
    currentL = (currentSprite->world.X - (currentSprite->width/2));
    currentR = (currentSprite->world.X + (currentSprite->width/2));
    currentT = (currentSprite->world.Y - (currentSprite->height/2));
    currentB = (currentSprite->world.Y + (currentSprite->height/2));

    controlledL = (controlledSprite->world.X - (controlledSprite->width/2));
    controlledR = (controlledSprite->world.X + (controlledSprite->width/2));
    controlledT = (controlledSprite->world.Y - (controlledSprite->height/2));
    controlledB = (controlledSprite->world.Y + (controlledSprite->height/2));
   
	/* Collision Algorithm */
    if((currentR > controlledL) &&  (currentL < controlledR) && 
	   (currentT < controlledB) && (currentB > controlledT)) 
    {     
      /*controlledSprite->color = RED;*/    /* sets sprite to red if collision detected */

	  /* if enemy collides from left */
	  if((controlledL <= currentL) && (controlledB > (currentT + collidingOffset))
		  && (controlledT < (currentB - collidingOffset))) 
	  {
		  isCollidingFromLeft = 1;
		  controlledSprite->world.X = (currentL - (controlledSprite->width/2));
	  }
      /* if enemy collides from right */
	  else if((controlledL >= currentL) && (controlledB > (currentT + collidingOffset)) 
		  && (controlledT < (currentB - collidingOffset))) 
	  {
		  isCollidingFromRight = 1;
		  controlledSprite->world.X = (currentR + (controlledSprite->width/2)) ;
	  }
	  /* if enemy collides from top */
	  else if((controlledT <= currentT))
	  {
		  isCollidingFromTop = 1;
		  controlledSprite->world.Y = (currentT - (controlledSprite->height/2)) ;
	  }
	  /* if enemy collides from bottom */
	  else if((controlledT >= currentT)) 
	  {
		  isCollidingFromBot = 1;  
		  controlledSprite->world.Y = (currentB + (controlledSprite->height/2)) ;
	  }
	  currentSprite = currentSprite->next;
	  return 1;
    }
    else
    {
      /*controlledSprite->color = GREEN;*/ /* sets sprite back to green if no collision */
	  /* points to the next sprite in the linklist, testing for collision*/
      currentSprite = currentSprite->next; 

	  /*booleans for isColliding*/
      isCollidingFromLeft = 0;
      isCollidingFromRight = 0;
      isCollidingFromTop = 0;
      isCollidingFromBot = 0;
    }
  }
   return 0;/* returns 0 if controlledSprite does not detect collision */
}

// Draws a line upward from the player sprite, stops drawing when it hits
// an enemy object.
//void MindControlRay(Sprite* character, Sprite *spriteList)
//{
//  /* Coordinates of Left, Right, Top, Bottom borders of the two squares */
//  int currentL;
//  int currentR;
//  int currentT;
//  int currentB;
//
//  currentL = (currentSprite->position.X);
//  currentR = (currentSprite->position.X + currentSprite->width);
//  currentT = (currentSprite->position.Y);
//  currentB = (currentSprite->position.Y + currentSprite->height);
//
//  int x = 0;
//  int y = 0;
//
//  Sprite *currentSprite = spriteList;
//
//
//  for(y = (dpScreenHeight / 2) - (character->height / 2) - 1; y > 0; --y)
//  {
//    if(y  )
//    {
//      *(screenBuffer + ((dpScreenWidth / 2) + (character->width / 2))
//        + y * dpScreenWidth) = 0x0000FF;       
//    }    
//    else
//    {
//      break;
//    }
//  }
//}